package cn.bisoft.java.service.ext.httpclient;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.bisoft.java.bean.entity.Drawing;
import cn.bisoft.java.bean.entity.Issue;
import cn.bisoft.java.bean.entity.Ticket;
import cn.bisoft.java.bean.message.ResponseMessageBody;
import cn.bisoft.java.bean.message.TransMessage;
import cn.bisoft.java.bean.message.TransType;
import cn.bisoft.java.bean.message.response.BalanceQueryResult;
import cn.bisoft.java.bean.message.response.BonusQueryResult;
import cn.bisoft.java.bean.message.response.DrawingQueryResult;
import cn.bisoft.java.bean.message.response.FillQueryResult;
import cn.bisoft.java.bean.message.response.LotteryRequestResult;
import cn.bisoft.java.bean.message.response.PrizeQueryResult;
import cn.bisoft.java.bean.message.response.TicketQueryResult;
import cn.bisoft.java.constants.Constants;
import cn.bisoft.java.service.ext.httpclient.common.impl.MessageConverterImpl;
import cn.bisoft.java.util.config.ConfigProvider;
import cn.bisoft.java.util.config.ConfigProviderFactory;

import com.hengpeng.common.msg.agentportal.MessageHelper;
import com.hengpeng.common.util.MessageLoger;

public class LotteryRequestFacade
{
	/*private static final long serialVersionUID = -3924223846414422902L;

	private static final Log logger = LogFactory.getLog(LotteryRequestFacade.class);

	private String counterId;

	private String facadeUrl;

	private String localPrivateKey;

	private String remotePublicKey;

	private String signType;

	public LotteryRequestFacade()
	{
		ConfigProvider provider = ConfigProviderFactory.getInstance(Constants.HP_COMMON_PROPERTIES_FILEPATH);
		this.counterId = (String) provider.getConfigByPrimaryKey(Constants.AGENTPORTAL_COUNTER_ID);
		this.facadeUrl = (String) provider.getConfigByPrimaryKey(Constants.AGENTPORTAL_ACCESS_URL);
		this.localPrivateKey = (String) provider.getConfigByPrimaryKey(Constants.AGENTPORTAL_PRIVATEKEY);
		this.remotePublicKey = (String) provider.getConfigByPrimaryKey(Constants.AGENTPORTAL_PUBLICKKEY);
		this.signType = (String) provider.getConfigByPrimaryKey(Constants.AGENTPORTAL_SIGNTYPE);
	}

	public LotteryRequestFacade(String counterId, String facadeUrl, String signType, String localPrivateKey,
			String remotePublicKey)
	{
		this.counterId = counterId;
		this.facadeUrl = facadeUrl;
		this.localPrivateKey = localPrivateKey;
		this.remotePublicKey = remotePublicKey;
		this.signType = signType;
	}

	public TransResult<BalanceQueryResult> balanceQuery(String version, long messageSeq, String frontGameId,
			String issueNumber) throws Exception
	{
		TransMessage tm = MessageBuiler.createBalanceQueryMessage(version, messageSeq, counterId, frontGameId,
				issueNumber);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_BALANCEQUERY_REQUEST);
		return new TransResult<BalanceQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(BalanceQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<BonusQueryResult> bonusQuery(String version, long messageSeq, String frontGameId,
			String issueNumber) throws Exception
	{
		TransMessage tm = MessageBuiler.createBonusQueryMessage(version, messageSeq, counterId, frontGameId,
				issueNumber);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_BONUSQUERY_REQUEST);
		return new TransResult<BonusQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(BonusQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<DrawingQueryResult> drawQuery(String version, long messageSeq, Set<String> drawingIds)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createDrawingQueryMessage(version, messageSeq, counterId, drawingIds);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_DRAWINGQUERY_REQUEST);
		return new TransResult<DrawingQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(DrawingQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<?> drawRequest(String version, long messageSeq, Set<Drawing> drawings) throws Exception
	{
		TransMessage tm = MessageBuiler.createDrawingRequestMessage(version, messageSeq, counterId, drawings);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_DRAWINGREQUEST_REQUEST);
		return new TransResult<Object>(doHttpRequest.getCode(), doHttpRequest.getMessage(), null, doHttpRequest
				.getRequestMessage(), doHttpRequest.getResponseMessage());
	}

	public TransResult<FillQueryResult> fillQuery(String version, long messageSeq, Set<String> fillIds)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createFillQueryMessage(version, messageSeq, counterId, fillIds);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_FILLQUERY_REQUEST);
		return new TransResult<FillQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(FillQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<Set<Issue>> issueQuery(String version, long messageSeq, String frontGameId, String issueNumber)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createIssueQueryMessage(version, messageSeq, counterId, frontGameId,
				issueNumber);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_ISSUEQUERY_REQUEST);
		return new TransResult<Set<Issue>>(doHttpRequest.getCode(), doHttpRequest.getMessage(), doHttpRequest
				.getReturnValue().getIssue(), doHttpRequest.getRequestMessage(), doHttpRequest.getResponseMessage());
	}

	public TransResult<TicketQueryResult> lotteryQuery(String version, long messageSeq, Set<String> ticketIds)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createLotteryQueryMessage(version, messageSeq, counterId, ticketIds);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_LOTTERYQUERY_REQUEST);
		return new TransResult<TicketQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(TicketQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<LotteryRequestResult> lotteryRequest(String version, long messageSeq, Set<Ticket> tickets)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createLotteryRequestMessage(version, messageSeq, counterId, tickets);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_LOTTERYREQUEST_REQUEST);
		return new TransResult<LotteryRequestResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(LotteryRequestResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<LotteryRequestResult> lotteryRequestById(String version, String messageSeq, Set<Ticket> tickets)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createLotteryRequestMessageById(version, messageSeq, counterId, tickets);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_LOTTERYREQUEST_REQUEST);
		return new TransResult<LotteryRequestResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(LotteryRequestResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<PrizeQueryResult> prizeQuery(String version, long messageSeq, String frontGameId,
			String issueNumber) throws Exception
	{
		TransMessage tm = MessageBuiler.createPrizeQueryMessage(version, messageSeq, counterId, frontGameId,
				issueNumber);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_PRIZEQUERY_REQUEST);
		return new TransResult<PrizeQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(PrizeQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	public TransResult<TicketQueryResult> ticketQuery(String version, long messageSeq, Set<String> ticketIds)
			throws Exception
	{
		TransMessage tm = MessageBuiler.createTicketQueryMessage(version, messageSeq, counterId, ticketIds);
		TransResult<ResponseMessageBody> doHttpRequest = doHttpRequest(facadeUrl, signType, remotePublicKey,
				localPrivateKey, tm, TransType.TRANSTYPE_TICKETQUERY_REQUEST);
		return new TransResult<TicketQueryResult>(doHttpRequest.getCode(), doHttpRequest.getMessage(),
				(TicketQueryResult) doHttpRequest.getReturnValue().getBody(), doHttpRequest.getRequestMessage(),
				doHttpRequest.getResponseMessage());
	}

	private TransResult<ResponseMessageBody> doHttpRequest(String facadeUrl, String signType, String remotePublicKey,
			String localPrivateKey, TransMessage tm, TransType transtype) throws Exception
	{
		Map<String, String> parameters = new HashMap<String, String>();
		String xml = MessageConverterImpl.getInstance().serialize(tm, transtype.value(), localPrivateKey, signType);
		parameters.put(Constants.MESSAGE_AGENTPORTAL_NAMING_TRANSTYPE, String.valueOf(transtype.value()));
		parameters.put(Constants.MESSAGE_AGENTPORTAL_NAMING_TRANSMESSAGE, xml);
		String rspMsg = HttpRequestProxy.doPost(facadeUrl, parameters, Constants.MESSAGE_AGENTPORTAL_ENCODING);
		if (rspMsg != null)
		{
			TransMessage rtntm = MessageConverterImpl.getInstance().deserialize(
					MessageHelper.getParameter(rspMsg, Constants.MESSAGE_AGENTPORTAL_NAMING_TRANSMESSAGE),
					transtype.obverse(), remotePublicKey, signType);
			ResponseMessageBody rspbody = (ResponseMessageBody) rtntm.getBody();
			if (!Constants.TRANSACTION_RESPONSE_CODE_SUCCESS.equalsIgnoreCase(rspbody.getCode()))
			{
				logger.error("HTTP������Ӧ����, �������{" + rspbody.getCode() + "}, ������Ϣ{" + rspbody.getMessage()
						+ "}, ������Ϣ{" + xml + "}.");
			}
			return new TransResult<ResponseMessageBody>(rspbody.getCode(), rspbody.getMessage(), rspbody, tm, rtntm);
		}
		else
		{
			throw new Exception("HTTP����ʧ��{�����쳣}.");
		}
	}*/
}